package parser;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;


public class XML2CSVParser {
	public static String XPATH_EXPRESSION_TIMESTAMP = "/contextML/ctxEls/ctxEl/timestamp";
	public static String XPATH_EXPRESSION_POSITION = "/contextML/ctxEls/ctxEl/dataPart";
	
	public static void save(String urlString, FileOutputStream xml)throws Exception{
		// Convert the string to a URL so we can parse it		    
		URL url = new URL(urlString);

	    ReadableByteChannel rbc = Channels.newChannel(url.openStream());
	    xml.getChannel().transferFrom(rbc, 0, 1 << 24);
		
	}
	
	public static void parse(String urlString, String CSV)throws Exception{
		//create file to append
		FileWriter out = new FileWriter(CSV,true);
		
		// Convert the string to a URL so we can parse it		    
	    URL url = new URL(urlString);

	    // create a urlconnection object
	    URLConnection urlConnection = url.openConnection();

	    // wrap the urlconnection in a bufferedreader
	    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

	    String line;

	    // read from the urlconnection via the bufferedreader
	    while ((line = bufferedReader.readLine()) != null){
	       System.out.println(line);
	    }
	    bufferedReader.close();
	    out.close();
		
	}

}
